<?php

namespace home\model;

use home\model\Model;

class CommentModel extends Model{
    /**
     * 获取所有评论
     */
    public function queryCommentReplayAll($params){
        $sql = "SELECT id,user_name,user_portrait,content,praise_count,time FROM comment WHERE state = 1 AND blog_id = :id ORDER BY praise_count DESC,is_top DESC ";
        $comment = $this->filter($this->p_queryAll($sql,$params));
        if($comment){
            $len = count($comment,0);
            for($i=0;$i<$len;$i++){
                $replay = [];
                $sql2 = "SELECT id,user_name,content,praise_count,time FROM comment_reply WHERE state = 1 AND comment_id = :id ORDER BY TIME DESC;";
                $replay[$i] = $this->filter($this->p_queryAll($sql2,$params));
                $comment[$i]['replay'] = $replay[$i];
            }
        }
    return $comment;

    }  
    /**
     * 发布评论
    */
    public function submitComment($params){
        $sql = "INSERT comment(user_name,user_mail,user_portrait,blog_id,content,time) 
        VALUES(:user_name,:user_mail,:user_portrait,:blog_id,:content,:time)";
        $this->p_execute($sql,$params);
        return $this->db->insertId;
    }

    /**
     * 过滤器
    */
    public function filter($data){
        $len = count($data,0);
        for($i=0;$i<$len;$i++){
            $data[$i]['time'] =  date("Y-m-d H:i ",$data[$i]['time']);       //时间戳转换时间
        }
        return $data;
    }
    /**
     * 赞数加一
    */
    public function addPrasise($params){
        $sql = 'UPDATE COMMENT SET praise_count = praise_count+1 WHERE state = 1 AND id= :id';
        $this->p_execute($sql,array('id'=>$params['id']));
        return $this->db->num;
    }
    /**
     * 
    */
    public function submitCommentReplay($params){
        $sql = "INSERT comment_reply(comment_id,user_name,blog_id,user_mail,content,time) 
        VALUES(:comment_id,:user_name,:blog_id,:user_mail,:content,:time)";
        $this->p_execute($sql,$params);
        return $this->db->num;
    }

}